Serialize models with exclude_unset, not exclude_defaults#208
Merged
Conversation
Member
Author
|
I just pinned a fairly huge Python function project to use this commit and I'm not seeing anything concerning. As expected my tests need a few updates for cases where the function code explicitly set a field to its defaults. Before this change those fields would be omitted on serialization - now they're emitted. |
This was referenced Jun 4, 2026
resource.update and update_status serialized Pydantic models with model_dump(exclude_defaults=True), which asks "is this field different from its default?". The correct question for server-side apply is "did the caller set this field?", which exclude_unset answers. exclude_defaults also regressed with newer datamodel-code-generator. It emits object defaults as a raw dict with validate_default=True instead of a default_factory. The default is validated into a model instance at construction, which doesn't compare equal to the declared dict default, so exclude_defaults fails to exclude it. Unset fields like spec.providerConfigRef then leaked into every composed resource. exclude_unset is immune to how a default is represented: a field the caller didn't touch is absent from model_fields_set. It also keeps fields the caller explicitly set to their default value, which is more correct under server-side apply, where setting a field claims ownership of it. The apiVersion and kind workaround stays. Functions build models with kwargs and rarely pass these, so they're unset and excluded either way. See crossplane#207 for more detail. Signed-off-by: Nic Cope <nicc@rk0n.org>
resource.update and resource.update_status serialize Pydantic models
with model_dump(exclude_unset=True), which doesn't pass by_alias=True.
A model field that carries a Pydantic alias is then serialized under its
Python attribute name rather than its alias, which is the resource's real
wire name.
This bites fields whose KRM name is a Python keyword or builtin.
datamodel-code-generator can't name a field bool, int, from, continue, or
schema, so it emits a bool_ attribute aliased to bool, int_ aliased to
int, and so on. When a function composes a resource that sets such a
field, update wrote the Python name into the desired resource:
data = source.model_dump(exclude_unset=True)
# -> {"bool_": True}, but the resource's field is "bool"
The composed resource then carried bool_: true instead of bool: true,
which doesn't match the resource's schema. The API server rejects it or
silently drops the unknown field, and the field the function set never
takes effect. This surfaced with Kubernetes Dynamic Resource Allocation,
whose device attribute value is a one-of over string, version, bool, and
int.
This change passes by_alias=True alongside exclude_unset=True in both
functions. It's a no-op for ordinary fields, which have no alias, and
corrects only the keyword-collision cases. It's also symmetric with how
pydantic deserializes these models: by default an aliased field is
populated only by its alias, so reads and writes now both speak wire
names.
Fixes crossplane#210.
Signed-off-by: Nic Cope <nicc@rk0n.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #207
Fixes #210
resource.updateandresource.update_statusserialized Pydantic models withmodel_dump(exclude_defaults=True). This PR makes two related corrections to how those functions serialize models.exclude_defaults→exclude_unsetexclude_defaultsasks "is this field different from its default?". The correct question for server-side apply is "did the caller set this field?", whichexclude_unsetanswers.exclude_defaultsalso regressed with newerdatamodel-code-generator, which emits object defaults as a raw dict withvalidate_default=True; the validated instance doesn't compare equal to the declared dict default, so unset fields likespec.providerConfigRefleaked into every composed resource.exclude_unsetis immune to how a default is represented (see crossplane/cli#64 (comment)) — a field the caller didn't touch is absent frommodel_fields_set.Add
by_alias=TrueNeither serialization passed
by_alias=True, so a field carrying a Pydantic alias was emitted under its Python attribute name rather than its alias — the resource's real wire name.datamodel-code-generatorcan't name a fieldbool,int,from,continue, orschema, so it emits abool_attribute aliased tobool, and so on. A function composing such a field wrotebool_: trueinstead ofbool: true, which doesn't match the resource's schema, so the API server rejects it or silently drops it. This surfaced with Kubernetes Dynamic Resource Allocation, whose device attribute value is a one-of overstring,version,bool, andint.Passing
by_alias=Trueis a no-op for ordinary fields and corrects only the keyword-collision cases. It's also symmetric with how pydantic deserializes these models: by default an aliased field is populated only by its alias, so reads and writes now both speak wire names. It's orthogonal to theexclude_unsetchange: one decides which fields are emitted, the other how they're named.I have: